package control.server;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import model.entity.Agent;
import model.entity.HeadQuater;
import model.entity.Task;
import net.sf.json.JSONSerializer;



import org.apache.http.ParseException;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import org.json.JSONException;
import org.json.JSONObject;

import com.sdicons.json.parser.JSONParser;

public class Response {
	private String status;
	private String statusMessage;
	private int secondsLeft;
	private int turn;
	private int money;
	private ArrayList<Agent> agents;
	private ArrayList<Task> tasks = new ArrayList<Task>();
	private ArrayList<HeadQuater> headQuarters;
	private int mapsize;
	private Map<Integer, Integer[]> roadblocks;
	
	Response(String request){
		
		/*JSONObject json = (JSONObject) JSONSerializer.toJSON( request );        
        status = json.getString("status");
        statusMessage = json.getString("statusMessage");
        secondsLeft = json.getInt("secondsLeft");
        turn =json.getInt("turn");
        money = json.getInt("money");
        
        System.out.println(money+status);*/
        
	
		int idxOfstatus = request.indexOf("status\"");
		int idxOfstatusMessage = request.indexOf("statusMessage\"");
		int idxOfsecondLeft= request.indexOf("secondsLeft\"");
		int idxOfturn = request.indexOf("turn\"");
		int idxOfmoney = request.indexOf("money\"");
		int idxOfagents = request.indexOf("agents\"");
		int idxOftasks = request.indexOf("tasks\"");
		int idxOfheadQuarters = request.indexOf("headQuarters\"");
		int idxOfmapsize = request.indexOf("mapSize\"");
		int idxOfroadBlocks = request.indexOf("roadBlocks\"");
		
		if(idxOfstatus!=-1)
			status=request.substring(idxOfstatus+9, request.indexOf("\"", idxOfstatus+9));
		if(idxOfstatusMessage!=-1)
			statusMessage=request.substring(idxOfstatusMessage+16, request.indexOf("\"", idxOfstatusMessage+16));
		if(idxOfsecondLeft!=-1)
			secondsLeft=Integer.parseInt(request.substring(idxOfsecondLeft+13, request.indexOf(",", idxOfsecondLeft+13)));
		if(idxOfturn!=-1)
			turn=Integer.parseInt(request.substring(idxOfturn+6, request.indexOf(",", idxOfturn+6)));
		if(idxOfmoney!=-1)
			money=Integer.parseInt(request.substring(idxOfmoney+7, request.indexOf(",", idxOfmoney+7)));
		if(idxOfmapsize!=-1)
			mapsize=Integer.parseInt(request.substring(idxOfmapsize+9,request.indexOf(",",idxOfmapsize+9)));

		String roadblocksstr = request.substring(idxOfroadBlocks+12, request.indexOf("}",idxOfroadBlocks+12)+1);
		try {
			roadblocks =
			        new ObjectMapper().readValue(roadblocksstr, HashMap.class);
		} catch (JsonParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	
	
		
		try {
			String tasksstring = request.substring(idxOftasks+8, request.indexOf("}",idxOftasks+8)+1);
			int actPos=tasksstring.length()+idxOftasks+8+1;
			
			while(request.substring(actPos-1,actPos).equals(",")){
				tasks.add(new Task(new ObjectMapper().readValue(tasksstring, HashMap.class)));
				tasksstring = request.substring(actPos, request.indexOf("}",actPos+1)+1);
				//System.out.println("new " + tasksstring);
				actPos=tasksstring.length()+actPos+1;
				//System.out.println(tasksstring);
				//System.out.println(tasks.get(0).getStatus());
			}
			
			//System.out.println("ch " +request.substring(actPos-1,actPos) );
			
			
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();	
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
	
		
	}
	
	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getStatusMessage() {
		return statusMessage;
	}

	public void setStatusMessage(String statusMessage) {
		this.statusMessage = statusMessage;
	}

	public int getSecondsLeft() {
		return secondsLeft;
	}

	public void setSecondsLeft(int secondsLeft) {
		this.secondsLeft = secondsLeft;
	}

	public int getTurn() {
		return turn;
	}

	public void setTurn(int turn) {
		this.turn = turn;
	}

	public int getMoney() {
		return money;
	}

	public void setMoney(int money) {
		this.money = money;
	}

	public ArrayList<Agent> getAgents() {
		return agents;
	}

	public void setAgents(ArrayList<Agent> agents) {
		this.agents = agents;
	}

	public ArrayList<Task> getTasks() {
		return tasks;
	}

	public void setTasks(ArrayList<Task> tasks) {
		this.tasks = tasks;
	}

	public ArrayList<HeadQuater> getHeadQuarters() {
		return headQuarters;
	}

	public void setHeadQuarters(ArrayList<HeadQuater> headQuarters) {
		this.headQuarters = headQuarters;
	}

	public int getMapsize() {
		return mapsize;
	}

	public void setMapsize(int mapsize) {
		this.mapsize = mapsize;
	}

	public Map<Integer, Integer[]> getRoadblocks() {
		return roadblocks;
	}

	public void setRoadblocks(Map<Integer, Integer[]> roadblocks) {
		this.roadblocks = roadblocks;
	}

	
}
